//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    cards: [],
    refresh: false,
    isFromSearch: true,
    searchLoading: false,
    searchLoadingComplete: false,
    callbackcount: 10,
    searchPageNum: 1
  },
  onPullDownRefresh: function() {
    this.setData({
      cards: [],
      searchPageNum: 1
    });
    this.myecarddata()
    wx.stopPullDownRefresh()
  },
  onReachBottom: function () {
    this.setData({
      searchLoading: true,
      refresh: true
    });
    var that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1,
        isFromSearch: false  //触发到上拉事件，把isFromSearch设为为false  
      });
      setTimeout(function () {
        that.setData({
          refresh: false
        });
        that.fetchSearchList();
      }, 500)
    }
  },
  //事件处理函数
  onLoad: function () {
    this.myecarddata()
  },
  myecarddata: function () {
    var that = this
    const requestTask = wx.request({
      method: "POST",
      header: { "content-type": "application/x-www-form-urlencoded" },
      data: {
        pageNo: '1',
        sessionId: app.globalData.sessionId
      },
      url: app.globalData.url + "/app/user/listJDecard",
      async: true,
      timeout: 10000,
      success: function (data) {
        console.log(data)
        that.setData({
          cards: data.data.data,
        });
      },
      error: function (textStatus) {
        console.error(textStatus);
      },
      complete: function (XMLHttpRequest, status) {
        if (status == 'timeout') {
          requestTask.abort()    // 超时后中断请求
          wx.showModal({
            title: '提示',
            content: '网络超时，请重试',
            showCancel: 'false',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  },
  fetchSearchList: function () {
    var that = this;
    var searchPageNum = that.data.searchPageNum,//把第几次加载次数作为参数  
      callbackcount = that.data.callbackcount; //返回数据的个数
    const requestTask = wx.request({
      method: "POST",
      header: { "content-type": "application/x-www-form-urlencoded" },
      data: {
        pageNo: searchPageNum,
        sessionId: app.globalData.sessionId
      },
      url: app.globalData.url + "/app/user/listJDecard",
      async: true,
      timeout: 10000,
      success: function (data) {
        console.log(data)
        console.log(that.data.searchPageNum)
        var num = 10 * (that.data.searchPageNum - 1)
        if (data.data.total > num) {
          var cards = that.data.cards.concat(data.data.data)
          that.setData({
            cards: cards, //获取数据数组  
            searchLoading: true   //把"上拉加载"的变量设为false，显示
          });
          console.log(that.data.cards)
        } else {
          that.setData({
            searchLoading: false  //把"上拉加载"的变量设为false，隐藏
          });
          wx.showToast({
            title: '没有更多商品了',
            icon: 'none',
            duration: 2000
          });
        }
      },
      error: function (textStatus) {
        console.error(textStatus);
      },
      complete: function (XMLHttpRequest, status) {
        if (status == 'timeout') {
          requestTask.abort()    // 超时后中断请求
          wx.showModal({
            title: '提示',
            content: '网络超时，请重试',
            showCancel: 'false',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  },
  searchScrollLower: function (e) {
    console.log(e)
    this.setData({
      searchLoading: true
    });
    var that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1,
        isFromSearch: false  //触发到上拉事件，把isFromSearch设为为false  
      });
      that.fetchSearchList();
    }
  },
  copycardnumber: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.code,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            console.log(res.data) // data
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            });
          }
        })
      }
    })
  },
  back: function(){
    wx.switchTab({
      url: '/pages/usercenter/usercenter'
    })
  },
  
})
